package com.surfacing.rcp.db.dao.mysql;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import org.apache.log4j.Logger;

import com.surfacing.rcp.db.DataSource;
import com.surfacing.rcp.db.Queries;
import com.surfacing.rcp.db.dao.StatDoctorDAO;
import com.surfacing.rcp.model.entity.StatDoctor;

public class StatDoctorDAOMySQL implements StatDoctorDAO {

	private static final Logger log = Logger.getLogger(StatDoctorDAOMySQL.class);
	// The data source to get the connection and the queries file

	private final DataSource source = DataSource.getInstance();
	private final Queries queries = Queries.getInstance();

	@Override
	public List<StatDoctor> getAllStatistics(Date startDate, Date endDate)
			throws SQLException {
		Connection connection = source.getConnection();
		try {
			final PreparedStatement query = connection.prepareStatement(queries
					.getStatment("create.StatistikArztDatum"));
			query.setDate(1, startDate);
			query.setDate(2, endDate);

			if (log.isDebugEnabled())
				log.debug("Statistics Query: " + query.toString());

			final ResultSet rs = query.executeQuery();
			// create the anforderung list and loop over the result
			List<StatDoctor> statEntries = new ArrayList<StatDoctor>();
			while (rs.next()) {
				StatDoctor statistics = new StatDoctor();
				statistics.setName(rs.getString("Arzt").replace("\n", ""));
				statistics.setAnzahl(rs.getString("Anzahl"));
				statEntries.add(statistics);
			}

			return statEntries;
		} finally {
			connection.close();
		}
	}

	@Override
	public List<StatDoctor> getAllStatistics() throws SQLException {
		Connection connection = source.getConnection();
		try {
			final PreparedStatement query = connection.prepareStatement(queries
					.getStatment("list.StatistikArztEntrys"));

			if (log.isDebugEnabled())
				log.debug("Statistics Query: " + query.toString());

			final ResultSet rs = query.executeQuery();
			// create the anforderung list and loop over the result
			List<StatDoctor> statEntries = new ArrayList<StatDoctor>();
			while (rs.next()) {
				StatDoctor statistics = new StatDoctor();
				statistics.setName(rs.getString("Arzt").replace("\n", ""));
				statistics.setAnzahl(rs.getString("Anzahl"));
				statEntries.add(statistics);
			}

			return statEntries;
		} finally {
			connection.close();
		}
	}
}
